/*
 * @Author: superRice
 * @Date: 2025-04-27 14:54:46
 * @LastEditors: superRice 1246333567@qq.com
 * @LastEditTime: 2025-04-28 16:27:53
 * @FilePath: /threejs-testing-dedicated/examples/testSprite2/CustomSpriteMaterial.ts
 * @Description:
 * Do your best to be yourself
 * Copyright (c) 2025 by superRice, All Rights Reserved.
 */
//@ts-nocheck
import * as THREE from 'three';
import SpriteVert from './shader/sprite.vert';
import SpriteFrag from './shader/sprite.frag';
const vertexShader = SpriteVert;
const fragmentShader = SpriteFrag;

export interface CustomSpriteMaterialParameters {
  color?: THREE.Color | string | number;
  map?: THREE.Texture;
  opacity?: number;
  transparent?: boolean;
  alphaTest?: number;
  depthTest?: boolean;
  depthWrite?: boolean;
}

export class CustomSpriteMaterial extends THREE.ShaderMaterial {
  public ViewVertexShader: string;
  public ViewFragmentShader: string;

  constructor(parameters?: CustomSpriteMaterialParameters) {
    //传入高度范围，以及最大缩放和最小缩放

    super({
      uniforms: {
        center: { value: new THREE.Vector2() },
        sizeScale: { value: 1 },
        zoom: { value: 1 },
        viewScale: { value: window.devicePixelRatio },
        rotation: { value: 0 },
        modelScale: { value: 1 },
        sizeAttenuation: { value: true },
        NearFarScalar: { value: new THREE.Vector4(100, 1, 1000, 0.5) }, //这个属性是根据相机到sprite的距离远近来搞得
        // heightRange: { value: new THREE.Vector2(0, 10) }, //这个属性是根据相机到地面的高度来搞得
      },
      vertexShader: vertexShader,
      fragmentShader: fragmentShader,
      side: THREE.DoubleSide,
    });

    if (parameters && parameters.color !== undefined) this.color = parameters.color;
    if (parameters && parameters.map !== undefined) this.texture = parameters.map;
    if (parameters && parameters.opacity !== undefined) this.opa = parameters.opacity;
    if (parameters && parameters.transparent !== undefined)
      this.transparent = parameters.transparent;
    if (parameters && parameters.alphaTest !== undefined) this.alphaTest = parameters.alphaTest;
    if (parameters && parameters.depthTest !== undefined) this.depthTest = parameters.depthTest;

    if (parameters && parameters.depthTest !== undefined) this.depthWrite = parameters.depthWrite;
    // this.uniforms.center = { value: new THREE.Vector2() };
    // this.uniforms.sizeScale = { value: 1 };
    // this.uniforms.zoom = { value: 1 };
    // this.uniforms.viewScale = { value: window.devicePixelRatio };
    // this.uniforms.rotation = { value: 0 };
    // this.uniforms.modelScale = { value: 1 };
    // this.uniforms.sizeAttenuation = { value: false };
    this.type = 'CustomSpriteMaterial'; //修改type，外面用了这个判断
  }

  setZoom(value: number) {
    this.uniforms.zoom.value = value;
  }
  setViewScale(value: number) {
    this.uniforms.viewScale.value = value;
  }
  setModelScale(value: number): void {
    this.uniforms.modelScale.value = value;
  }
  setRotation(value: number) {
    this.uniforms.rotation.value = value;
  }
  setSizeAttenuation(value: boolean) {
    this.uniforms.sizeAttenuation.value = value;
  }

  get sizeAttenuation() {
    return this.uniforms.sizeAttenuation.value;
  }

  get color() {
    return this.uniforms.diffuse === undefined ? undefined : this.uniforms.diffuse.value;
  }

  set color(value: THREE.Color | string | number) {
    let color: THREE.Color;
    if (value instanceof THREE.Color) color = value;
    else color = new THREE.Color(value);
    this.uniforms.diffuse = { value: color };
  }

  get texture() {
    return this.uniforms.map === undefined ? undefined : this.uniforms.map.value;
  }

  set texture(value: THREE.Texture) {
    this.uniforms.map = { value: value };
  }

  /**
   * 由于opacity属性无效，代替opacity属性
   */
  get opa() {
    return this.uniforms.opacity.value;
  }

  set opa(value: number) {
    this.uniforms.opacity = { value };
  }
}
